
package com.android.volley.toolbox.ex;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.dg.acb.utils.LogUtil;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.http.RequestEntity;
import com.android.volley.toolbox.HttpHeaderParser;

public abstract class NBEntityRequest<Result> extends Request<Result> {
    private RequestEntity mRequestEntity;
    private Map<String, String> mHeaders = new HashMap<String, String>();
    private final Listener<Result> mListener;

    /**
     * Creates a new POST request.
     * 
     * @param url URL to fetch the string at
     * @param rEntity RequestEntity
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public NBEntityRequest(String url, RequestEntity rEntity,
            Listener<Result> listener, ErrorListener errorListener) {
        this(Method.POST, url, rEntity, listener, errorListener);
    }

    public NBEntityRequest(int method, String url, RequestEntity rEntity,
            Listener<Result> listener, ErrorListener errorListener) {
        super(method, url, errorListener);
        if (LogUtil.DEBUG) {
            LogUtil.d("url=>" + url);
        }
      //  mRequestEntity = Objects.requireNonNull(rEntity);
     //   mListener = Objects.requireNonNull(listener);
          mRequestEntity =rEntity;
         mListener =listener;
        setRetryPolicy(new DefaultRetryPolicy(5000, 20000, 1,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }

    public RequestEntity getHttpEntity() {
        return mRequestEntity;
    }

    @Override
    public String getBodyContentType() {
        return mRequestEntity.getContentType();
    }

    public String getContentEncoding() {
        return mRequestEntity.getContentEncoding();
    }

    public void addHeader(String key, String value) {
        if (LogUtil.DEBUG) {
            LogUtil.d("addHeader:" + key + " => " + value);
        }
        mHeaders.put(key, value);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return mHeaders;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
        mRequestEntity.writeTo(out);
    }

    @Override
    protected void deliverResponse(Result response) {
        if (mListener != null) {
            mListener.onResponse(response);
        }
    }

    protected Response<Result> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, getContentEncoding()));
            if (LogUtil.DEBUG) {
                LogUtil.d("response:" + jsonString);
            }
            return Response.success(handlerResponse(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }catch (ParseError e) {
            return Response.error(new ParseError(e));
        }
    }

    protected abstract Result handlerResponse(String jsonString) throws ParseError;
}
